package jb;

import java.io.Serializable;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import edu.model.Usuario;


public class LoginJB  implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Double cve;
	private String pwd;
	
	public Double getCve(){return cve;}
	public void setCve(Double value){cve = value;}
	public String getPwd(){return pwd;}
	public void setPwd(String value){pwd = value;}
	
	public String login()
	{
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Usuario oUsu = new Usuario();
		 
		String sRet = "success";
		Firmado oFirm;
		
		oUsu.setId(cve);
		oUsu.setContrasena(pwd);
		try{
			//if (oUsu.buscarCvePwd()){
			if(oUsu.buscarCvePwd()){
				oFirm = new Firmado();
				oFirm.setUsuario(oUsu);
				HttpSession session =
					(HttpSession)facesContext.getExternalContext().
					getSession(false);
				session.setAttribute("oFirm", oFirm);
			}
			else{
				sRet = "failure";
			}
		}catch(Exception e){
			sRet = "error";
			e.printStackTrace();
		}
		return sRet;
	}
	
	public String isLogged(){
		String action = "success";
		Firmado oFirm;
		FacesContext facesContext = FacesContext.getCurrentInstance();
		
		HttpSession session =
			(HttpSession)facesContext.getExternalContext().
			getSession(false);
		
		oFirm = (Firmado)session.getAttribute("oFirm");
		if (oFirm == null){
			action = "faulire";
		}
		
		return action;
	}

}
